﻿namespace EFS.RC.CC.Tools.Repeater
{
	/// <summary>
	/// Callback
	/// </summary>
	internal class Callback : EFS.Common.Async.RepeaterSlot
	{
		#region Delegates

		// delegate
		public delegate void Delegate();

		// Callback delegate
		private Delegate callbackDelegate;

		#endregion

		#region Constructor

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="conversation"></param>
		/// <param name="remoteEndpoint"></param>
		public Callback(
			System.UInt16 cycles,
			EFS.RC.CC.Tools.Repeater.Callback.Delegate callbackDelegate)
			: base(cycles)
		{
			// check
			EFS.Common.Verify.Reference(callbackDelegate, "callbackDelegate");

			// sets
			this.callbackDelegate = callbackDelegate;

			// log
			// commented for performance ~ EFS.EventLog.Log.LogInfo("SupervisorContext", this);
		}

		#endregion

		#region Usage

		/// <summary>
		/// Describe this object into a nice formated string
		/// </summary>
		/// <returns></returns>
		public override System.String Describe()
		{
			return "EFS.RC.CC.Tools.Repeater.Callback delegate";
		}

		#endregion

		#region Runtime

		/// <summary>
		/// Unhandled exception
		/// </summary>
		/// <param name="ex"></param>
		protected override void OnException(System.Exception ex)
		{
			// check
			EFS.Common.Verify.Reference(ex, "ex");

			// log
			EFS.EventLog.Log.LogException(ex, this);
		}

		/// <summary>
		/// Repeate action
		/// </summary>
		protected override void RepeateAction()
		{
			callbackDelegate();
		}

		#endregion
	}
}
